package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	未完成
 * http://www.ntjoy.com/vod/xwsph/nttv1/mtdfs/2013/05/2013-05-06223546.html  江海明珠网
 * 	
 * @author KK
 *
 */
public class NtjoyNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/
		"http://www.ntjoy.com/.*[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]+.html",
		"http://www.ntjoy.com/.*/[0-9]*/[0-9]*/[0-9]*-[0-9]*-[0-9]*.html",
	};	
	
	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}		
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();	
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);				
		news.setUrl(url);	
		
		title =doc.select("span[style=font-size:25px; font-weight:bold; line-height:30px; color:#333333]").text();	
		if (StringUtils.isNullOrEmpty(title)){
			title =doc.select("title").text();	
			if (title!=null)title=StringUtils.substringBefore(title, "_");
			if(StringUtils.isBlank(title)){
				title =doc.select("h1").text();	
			}
		}		
		content=HtmlCleaner.getContentHtml(url,doc.select("div.lbyones"));	
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".nph_set_title p"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("li#contentarea"));			
		}
		
		if(StringUtils.isBlank(title)){
			title=doc.select(".nph_set_title h1").text();
		}
		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div.miantopword").text();
		if(StringUtils.isBlank(authorStr)){
			authorStr=doc.select("li:containsOwn(来源)").text();
		}
		author=StringUtils.substringBetween(authorStr, "来源："," ");
		if(StringUtils.isBlank(author)){
			author=StringUtils.substringBetween(authorStr, "来源:", " ");
		}
	
		date=DateUtils.matchDate(doc.select("html body div.container div.newsleft690 div.miantopword ul li p span").text());
		if (date==null){
			date=DateUtils.matchDate(doc.select("div:containsOwn(发布时间)").text());
			if (date==null){
				date=DateUtils.matchDate(doc.select("div.miantopword").text());
				if (date==null){
					date=DateUtils.matchDate(doc.select("li:containsOwn(发布时间)").text());
					if (date==null){
						date=DateUtils.matchDate(doc.select(".nph_set_title span").text());
					}
				}
			}
		}
		if (StringUtils.isNotBlank(title) && date!=null){			
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);	
			news.setType(1);	
		}
		return news;
	}	
	public static void main(String[] args) {
		String url="http://www.ntjoy.com/news/vod/xwsph/nttv1/nttv/2015/11/2015-11-05447089.html";
		NtjoyNewsAnalyse analyse=new NtjoyNewsAnalyse();		
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
